package com.zy.chat.util;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.chat.service.SaveGroupMessageService;
import com.zy.chat.service.SaveMessageService;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * Redis通用工具类
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SaveMessageService saveMessageService;

    @Autowired
    private SaveGroupMessageService saveGroupMessageService;


    private String touser;

    private  String username;
    private  String content;
    private  String time;
    private String type;

    // =============================common============================
    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key   键
     * @param map  项
     * @return true 成功 false失败
     */


    public boolean hset(String key, Map map) {
        try {
            JSONObject jsonObject = new JSONObject(map);
            String json=jsonObject.toString();
            redisTemplate.opsForList().leftPush(key,json);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean hsetadd(String key,Map map) {
        try {
            JSONObject jsonObject = new JSONObject(map);
            String json=jsonObject.toString();
            redisTemplate.opsForList().leftPush(key,json);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Scheduled(initialDelay = -1,fixedRate = 5000)
    public void savemessages() throws JSONException {
        Set<String> keys=redisTemplate.keys("*");
        if(!keys.equals(null)){
            for(String key:keys){
                switch (key){
                    case "chat:singemsg":
                        List<String> objs=redisTemplate.opsForList().range(key,0,-1);
                        Iterator<String> it = objs.iterator();
                        while (it.hasNext()) {
                            JSONObject jsonObject=JSON.parseObject(it.next());
                             touser= (String) jsonObject.get("touser");

                            username=(String) jsonObject.get("username");
                            content=(String) jsonObject.get("content");
                            time=(String) jsonObject.get("time");
                            type=(String) jsonObject.get("type");
                            saveMessageService.save(touser,username,time,content,type);
                        }
                        break;
                    case "chat:groupmsg":
                        List<String> gobjs=redisTemplate.opsForList().range(key,0,-1);
                        Iterator<String> git = gobjs.iterator();
                        while (git.hasNext()) {
                            JSONObject jsonObject=JSON.parseObject(git.next());
                             touser= (String) jsonObject.get("touser");
                             username=(String) jsonObject.get("username");
                             content=(String) jsonObject.get("content");
                             time=(String) jsonObject.get("time");
                             type=(String) jsonObject.get("type");
                             saveGroupMessageService.save(touser,username,time,content,type);
                        }
                        break;
                }




                redisTemplate.delete(key);
                System.out.println(key+"已删除");
            }
        }else{

        }

    }
}
